<?php
class Folder_picture extends MY_Model {

   	var $table_name = 'folder_picture';
	//var $primary_key = 'id';	
	function update_order($order,$folder_id,$picture_id)
    {
		$rows = $this->get_by_field(array('folder_id'=>$folder_id,'picture_id'=>$picture_id));
		$curr = $rows['0'];unset($rows);
		if($curr['order']>$order){
			$sub = $curr['order']-1;
			$this->db->query("UPDATE {$this->table_name} SET `order` = `order` + 1 WHERE `folder_id` = $folder_id AND `order` BETWEEN $order AND $sub");
		}elseif($curr['order']<$order){
			$inf = $curr['order']+1;
			$this->db->query("UPDATE {$this->table_name} SET `order` = `order` - 1 WHERE `folder_id` = $folder_id AND `order` BETWEEN $inf AND $order");
		}
		return $this->db->update($this->table_name, array('order'=>$order), array('folder_id'=>$folder_id,'picture_id'=>$picture_id));
    }
	function delete_by_field($field_value=array()){//$field_value = array('field_name'=>'value'); 	
		$curr = $this->get_by_field($field_value);
		foreach($curr as $row){
        	$this->db->query("UPDATE {$this->table_name} SET `order` = `order` - 1 WHERE `order` > {$row['order']} AND `folder_id` = {$row['folder_id']}");
		}
        return $this->db->delete($this->table_name, $field_value); 
    }
}